using System;
using System.Linq;
using CMS.api.Entity;
using CMS.api.Model;
using CMS.api.Repository;
using CMS.api.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace CMS.api.Controllers
{
    /// <summary>
    /// 设置路由定义的规则 以控制器前的命名作为该接口的访问路径
    /// </summary>
    [ApiController]
    [Route("[controller]")]
    /// <summary>
    /// 设置该接口是否需要传入token才能访问 (需要用到时可以使用)
    /// </summary>
    // [Authorize]
    public class UsersController : ControllerBase
    {
        // 数据库的增删改
        private ICURDable<Users> _db;

        // 用于保存token的配置信息
        private TokenParameter _tokenParameter;

        // 用于读取配置文件的信息
        private readonly IConfiguration _configuration;

        public UsersController(
            IConfiguration configuration,
            ICURDable<Users> db
        )
        {
            _db = db;
            _configuration = configuration;
            _tokenParameter =
                _configuration
                    .GetSection("TokenParameter")
                    .Get<TokenParameter>();
        }

        // 注册用户
        [HttpPost]
        public dynamic RegisterUser(NewUser model)
        {
            var username = model.Username.Trim();
            var password = model.Password.Trim();

            
            var u = _db.Table.Where(x => x.Username == username).FirstOrDefault();

            // 如果用户不存在,并且用户名不为空,那么就创建用户
            if (u == null && string.IsNullOrEmpty(username))
            {
                var user = new Users
                {
                    Username = username,
                    Password = password,
                    IsAdmina = false
                };
                _db.Inster(user);
                var res = new
                {
                    Code = 200,
                    Data = user,
                    Msg = "注册成功"
                };
                return JsonHelper.Serialize(res);
            }
            else
            {
                var res = new
                {
                    Code = 200,
                    Data = "",
                    Msg = "用户已存在或用户名为空"
                };
                return JsonHelper.Serialize(res);
            }



        }


        //登录验证并签发Token
        // [AllowAnonymous]
        [HttpPost, Route("GetToken")]
        public dynamic GetToken(NewUser model)
        {
            //用户名
            var username = model.Username.Trim();

            // 用户密码
            var password = model.Password.Trim();

            var user = _db.Table.Where(x => x.Username == username && x.Password == password).FirstOrDefault();

            // 判断是否找到用户
            if (user == null)
            {
                var tmp = new { Code = 1004, Data = "", Msg = "用户不存在=_=" };
                return JsonHelper.Serialize(tmp);
            }
            else
            {
                // 如果找到该用户,那么进行判断他是否被删除,没有删除那么登录登录成功,并签发token
                if (user.IsDeleted == true)
                {
                    var tmp2 =
                        new { Code = 1004, Data = "", Msg = "用户不存在=_=" };
                    return JsonHelper.Serialize(tmp2);
                }
                else
                {
                    var token =
                        TokenHelper.GenerateToken(_tokenParameter, user);
                    var refreshToken = "123456";
                    var tmp3 =
                        new
                        {
                            Code = 200,
                            Data =
                                new
                                {
                                    Data = user,
                                    Token = token,
                                    RefreshToken = refreshToken
                                },
                            Msg = "登录成功^_^"
                        };
                    return JsonHelper.Serialize(tmp3);
                }
            }
        }
    }
}
